<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/themes/bootstrap/easyui.css" />
<link type="text/css"
	href="${pageContext.request.contextPath}/themes/icon.css"
	rel="stylesheet">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
$(function(){
	$('#dg').datagrid({    
	    url:'user',    
	    columns:[[    
	        {field:'ck',width:150,checkbox:true},  
	        {field:'uId',title:'用户ID',width:150,hidden:true},    
	        {field:'uName',title:'姓名',width:150}, 
	        {field:'uAccount',title:'账号',width:150},    
	        {field:'uPsw',title:'密码',width:150},    
	        {field:'rid',title:'角色ID',width:150,hidden:true},
	        {field:'rName',title:'角色名',width:150},
	    ]],
	    pagination:true,
	    rownumbers:true,
	    toolbar: [{
			iconCls: 'icon-add',
			text:'增加',
			handler: function(){
				$("#add").window('open');
			}
		},'-',{
			iconCls: 'icon-remove',
			text:'删除',
			handler: function(){alert('帮助按钮')}
		},'-',{
			iconCls: 'icon-edit',
			text:'编辑',
			handler: function(){
				var sel = $('#dg').datagrid('getSelections');
				if(sel.length==1){
					var upacc = sel[0].uAccount;
					var uppsw = sel[0].uPsw;
					var upname = sel[0].uName;
					var uprole = sel[0].rid;
					
					$("#upaccount").textbox('setText',upacc);
					$("#uppsw").textbox('setText',uppsw);
					$("#upname").textbox('setText',upname);
					$("#uprole").combobox('setValue',uprole);
					$("#update").window('open');
				}else{
					$.messager.alert('提示','请仅选择一条信息进行修改');  
				}
			}
		}]

	});  
	
	$("#add").window({    
	    width:400,    
	    height:300,    
	    modal:true,
	    title:'增加用户信息',
	    closed:true,
	});  
	
	$("#cancel").linkbutton({
		onClick:function(){
			$("#add").window('close');
		}
	})
	
	$("#confirm").linkbutton({
		onClick:function(){
			var acc = $("#account").textbox('getText');
			var psw = $("#psw").textbox('getText');
			var name = $("#name").textbox('getText');
			var role = $("#role").combobox('getValue');
			$.ajax({
				url:'adduser',
				data:{"account":acc,"psw":psw,"name":name,"role":role},
				type:'post',
				success:function(flag){
					if(flag=='-1'){
						$.messager.alert('提示','请填写完整');  
					}else if(flag=='1'){
						$.messager.alert('提示','增加成功');
						$("#add").window('close');
						$("#account").textbox('clear');
						$("#psw").textbox('clear');
						$("#name").textbox('clear');
						$("#role").combobox('clear');
						$('#dg').datagrid('reload');
					}else{
						$.messager.alert('提示','增加失败');  
					}
				}
			})
		}
	})
	
	$("#update").window({    
	    width:400,    
	    height:300,    
	    modal:true,
	    title:'修改用户信息',
	    closed:true,
	});  
	
	$("#upcancel").linkbutton({
		onClick:function(){
			$("#update").window('close');
		}
	})
	
	$("#upconfirm").linkbutton({
		onClick:function(){
			var acc = $("#upaccount").textbox('getText');
			var psw = $("#uppsw").textbox('getText');
			var name = $("#upname").textbox('getText');
			var role = $("#uprole").combobox('getValue');
			var id = ($('#dg').datagrid('getSelections'))[0].uId;
			$.ajax({
				url:'updateuser',
				data:{"account":acc,"psw":psw,"name":name,"role":role,"uId":id},
				type:'post',
				success:function(flag){
					if(flag==1){
						$.messager.alert('提示','增加成功'); 
						$("#update").window('close');
						$('#dg').datagrid('reload');
					}else{
						$.messager.alert('提示','修改失败'); 
					}
				}
			})
		}
	})

	
	$('#role').combobox({    
		url:'roleselect',    
	    valueField:'id',    
	    textField:'text',
	    width:180,
	    editable:false,
	   	panelHeight:'auto'
	});  
	
	$('#uprole').combobox({    
		url:'roleselect',    
	    valueField:'id',    
	    textField:'text',
	    width:180,
	    editable:false,
	   	panelHeight:'auto'
	});  

})
</script>
<body>
	<table id="dg" ></table>  
	<div id="add">
		<div  style="margin-top: 40px;margin-left: 80px">
			账号<input id='account' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>
			密码<input id='psw' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>
			姓名<input id='name' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>		
			角色<input id="role" name="role">  	<br/><br/>	
			<div style="margin-left: 50px"> 
				<a href="#" id='confirm' class="easyui-linkbutton" style="width: 60px">确定</a>  
				<a href="#" id='cancel' class="easyui-linkbutton" style="width: 60px">取消</a> 				
			</div>
		</div>
	</div> 
	<div id="update">
		<div  style="margin-top: 40px;margin-left: 80px">
			账号<input id='upaccount' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>
			密码<input id='uppsw' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>
			姓名<input id='upname' class="easyui-textbox" data-options="" style="width:180px"><br/><br/>		
			角色<input id="uprole" name="role">  	<br/><br/>	
			<div style="margin-left: 50px"> 
				<a href="#" id='upconfirm' class="easyui-linkbutton" style="width: 60px">确定</a>  
				<a href="#" id='upcancel' class="easyui-linkbutton" style="width: 60px">取消</a> 				
			</div>
		</div>
	</div>   

</body>
</html>